package invest.data.getdata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import invest.data.Data;
import invest.pojo.datapojo.ResearchReport;
import invest.utils.DateUtil;
import invest.utils.HttpRequest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class ResearchReportData implements Data<List<ResearchReport>> {
    String url = "http://reportapi.eastmoney.com/report/list?";
    @Override
    public List<ResearchReport> getData() throws UnsupportedEncodingException {
        Map<String, String> param = new HashMap<>();
        Date now = new Date();
        Date lastyear = DateUtil.getLastyear(now);
        param.put("industryCode", "*");
        param.put("pageSize", "300");
        param.put("beginTime", DateUtil.getStringDate(lastyear));
        param.put("endTime", DateUtil.getStringDate(now));
        param.put("pageNo", "1");
        param.put("qType", "0");
        param.put("_", String.valueOf(System.currentTimeMillis()));
        String paramlink = HttpRequest.createLinkStringByGet(param);
        String link = this.url + paramlink;
        String result = HttpRequest.sendGet(link, null);
        JSONObject jb = JSONObject.parseObject(result);
        JSONArray data = jb.getJSONArray("data");
        List<ResearchReport> researchReports = new ArrayList<>();
        for (int i = 0; i <data.size() ; i++) {
            ResearchReport researchReport = JSON.toJavaObject(data.getJSONObject(i), ResearchReport.class);
            researchReports.add(researchReport);
        }
        return researchReports;
    }


}
